package controllers

import (
	"ShopSupplierApi/models"
	"ShopSupplierApi/services"
	"ShopSupplierApi/utils"
)

//CondisController 优惠活动
type CondisController struct {
	LcfController
}

// @Title 商品分类信息
// @Param	p	body	models.Page	true	"分页数据信息"
// @Success 200
// @router /catList [post]
func (c *CondisController) CatList() {
	err := c.ParseBodyToPage()
	if err != nil {
		return
	}
	c.JSONPageData(services.CatList(c.Data["page"].(*models.Page)))
}

// @Title 货品列表
// @Param	sid	query	uint64	true "供应商ID"
// @Param	p body models.Page	true	"数据分页信息"
// @Success 200
// @router /productList [post]
func (c *CondisController) ProductList() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	err = c.ParseBodyToPage()
	if err != nil {
		return
	}
	c.JSONPageData(services.ConDisServ.ProductList(uint64(sid), c.Data["page"].(*models.Page)))
}

// @Title 优惠列表
// @Param	sid	query	uint64	true "供应商ID"
// @Param	p body models.Page	true	"数据分页信息"
// @Success 200
// @router /disList [post]
func (c *CondisController) DiscountList() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	err = c.ParseBodyToPage()
	if err != nil {
		return
	}
	c.JSONPageData(services.ConDisServ.DiscountOrConditionList(uint64(sid), false, c.Data["page"].(*models.Page)))
}

// @Title 条件列表
// @Param	sid	query	uint64	true "供应商ID"
// @Param	p body models.Page	true	"数据分页信息"
// @Success 200
// @router /conList [post]
func (c *CondisController) CounditionList() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	err = c.ParseBodyToPage()
	if err != nil {
		return
	}
	c.JSONPageData(services.ConDisServ.DiscountOrConditionList(uint64(sid), true, c.Data["page"].(*models.Page)))
}

// @Title 优惠活动列表
// @Param	sid	query	uint64	true "供应商ID"
// @Param	p body models.Page	true	"数据分页信息"
// @Success 200
// @router /condisList [post]
func (c *CondisController) ConditionDiscountList() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	err = c.ParseBodyToPage()
	if err != nil {
		return
	}
	c.JSONPageData(services.ConDisServ.ConditionDiscountList(uint64(sid), c.Data["page"].(*models.Page)))
}

// @Title 添加优惠
// @Param	sid	query	uint64	true	"供应商ID"
// @Param	dis	body	models.Discount	true	"优惠信息"
// @router /addDis [post]
func (c *CondisController) AddDiscount() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	var dis models.Discount
	err = c.JSONParseBody(&dis)
	if err != nil {
		return
	}
	dis.SupplierID = uint64(sid)
	c.JSONSuccess(services.ConDisServ.AddDiscount(dis))
}

// @Title 添加条件
// @Param	sid	query	uint64	true	"供应商ID"
// @Param	cond	body	models.Condition	true	"添加信息"
// @router /addCond [post]
func (c *CondisController) AddCoudition() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	var cond models.Condition
	err = c.JSONParseBody(&cond)
	if err != nil {
		return
	}
	cond.SupplierID = uint64(sid)
	c.JSONSuccess(services.ConDisServ.AddCondition(cond))
}

// @Title 添加优惠活动
// @Param	sid	query	uint64	true	"供应商ID"
// @Param	condis	body	models.ConditionDiscount	true	"活动信息"
// @router /addConDis [post]
func (c *CondisController) AddConDis() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	var condis models.ConditionDiscount
	err = c.JSONParseBody(&condis)
	if err != nil {
		return
	}
	condis.SupplierID = uint64(sid)
	c.JSONSuccess(services.ConDisServ.AddConditionDiscout(condis))
}
